const express = require('express');
const router = express.Router();
const { asyncHandle } = require('../getSendResult');
const { createFiles, findFiles, findFilesAllCount, findFile, updateFile, deleteFile } = require('../../services/files');

// 创建文件
router.post('/createFiles', asyncHandle(
    async (req, res, next) => {
        const name = req.body.name;
        const content = req.body.content;
        const type = req.body.type;
        const folderId = req.body.folderId;
        return await createFiles(name, content, type, folderId);
    }
))

// 查询文件分页
router.get('/findFiles', asyncHandle(
    async (req, res, next) => {
        const page = req.query.page || 1;
        const limit = req.query.limit || 100;
        const folderId = req.query.folderId;
        return findFiles(page, limit, folderId);
    }
))

// 查询所有文件
router.get('/findFilesAllCount', asyncHandle(
    async (req, res, next) => {
        const folderId = req.query.folderId;
        return findFilesAllCount(folderId);
    }
))

// 查询单个文件
router.get('/findFile', asyncHandle(
    async (req, res, next) => {
        const folderId = req.query.folderId;
        const fileId = req.query.fileId;
        return findFile(folderId, fileId);
    }
))

// 更新单个文件
router.post('/updateFile', asyncHandle(
    async (req, res, next) => {
        const folderId = req.body.folderId;
        const fileId = req.body.fileId;
        const name = req.body.name;
        const content = req.body.content;
        return updateFile(folderId, fileId, name, content);
    }
))

router.get('/deleteFile',asyncHandle(
    async (req,res,next)=>{
        const fileId = req.query.fileId;
        return deleteFile(fileId);
    }
))

module.exports = router;